package service

import (
	"gin/common/types"
	Mysql "gin/database"
	"gin/model"
	"gin/validate"
	uuid "github.com/satori/go.uuid"
	teamones_helper "github.com/teamones-open/go-helper"
	"strconv"
	"time"
)

// 通过文件名查找工具
func FindToolsByFileName(fileName string) (res model.Tools) {
	Mysql.DB.Where("file_name = ?", fileName).Take(&res)
	return
}

// 通过文件名查找工具
func FindToolsByID(id int64) (res model.Tools) {
	Mysql.DB.Where("id = ?", id).Take(&res)
	return
}

// 保存工具数据
func CreateTools(Data validate.AddToolData) (resData model.Tools, err error) {
	var toolsModel model.Tools
	toolsModel.Name = Data.Name
	toolsModel.TenantId = Data.TenantId
	toolsModel.DCC = Data.DCC
	toolsModel.Category = Data.Category
	toolsModel.Description = Data.Description
	toolsModel.FileName = Data.FileName
	toolsModel.Arguments = Data.Arguments
	toolsModel.Icon = Data.Icon
	toolsModel.StyleSheet = Data.StyleSheet
	toolsModel.Tips = Data.Tips
	toolsModel.Parent = Data.Parent
	toolsModel.Mode = Data.Mode
	toolsModel.Type = Data.Type

	// 创建时间
	toolsModel.Created = types.TstampTime(time.Now().Unix())

	// uuid
	toolsModel.Uuid = uuid.NewV4().String()

	_, err = toolsModel.Insert()

	return toolsModel, err
}

// 更新工具
func UpdateTool(Data validate.UpdateToolData) (resData model.Tools, err error) {
	var toolsModel model.Tools

	// 查询当前工具id是否存在
	if Data.Id > 0 {
		toolsModel, err = toolsModel.FindById(Data.Id)
		if err != nil {
			return
		}
	}

	if Data.Name != "" {
		toolsModel.Name = Data.Name
	}

	if Data.DCC != "" {
		toolsModel.DCC = Data.DCC
	}

	if Data.Category != "" {
		toolsModel.Category = Data.Category
	}

	if Data.Description != "" {
		toolsModel.Description = Data.Description
	}

	if Data.FileName != "" {
		toolsModel.FileName = Data.FileName
	}

	if Data.Arguments != "" {
		toolsModel.Arguments = Data.Arguments
	}

	if Data.Icon != "" {
		toolsModel.Icon = Data.Icon
	}

	if Data.StyleSheet != "" {
		toolsModel.StyleSheet = Data.StyleSheet
	}

	if Data.Tips != "" {
		toolsModel.Tips = Data.Tips
	}

	if Data.Parent != "" {
		toolsModel.Parent = Data.Parent
	}

	if Data.Mode != "" {
		toolsModel.Mode = Data.Mode
	}

	if Data.Type != "" {
		toolsModel.Type = Data.Type
	}

	err = toolsModel.Update()

	return toolsModel, err
}

// 查询多条工具
func SelectTools(Param teamones_helper.SelectQueryParam) (total int, resData []model.ToolsFormat, err error) {
	var toolsModel model.Tools

	total, resData, err = toolsModel.Select(Param)

	return
}

// 创建DCC工具
func CreateDCCTools(Data validate.AddDCCToolData) (resData model.DCCTools, err error) {
	var DCCToolsModel model.DCCTools
	DCCToolsModel.TenantId = Data.TenantId
	DCCToolsModel.DCC = Data.DCC
	DCCToolsModel.Position = Data.Position
	DCCToolsModel.ToolsId = Data.ToolsId

	_, err = DCCToolsModel.Insert()

	return DCCToolsModel, err
}

// 删除工具
func RemoveToolsById(id int64) (err error) {
	//  1、先删除dcc所有当前工具
	var DCCToolsModel model.DCCTools
	err = DCCToolsModel.RemoveToolsFromDCCByToolsId(id)
	if err != nil {
		return
	}

	// 2、删除该工具
	var ToolsModel model.Tools
	err = ToolsModel.RemoveToolsById(id)
	return
}

// 从DCC工具架移除工具
func RemoveToolsFromDCCById(id int64) (err error) {
	var DCCToolsModel model.DCCTools

	err = DCCToolsModel.RemoveToolsFromDCCById(id)
	return
}

// 查询指定DCC工具列表
func GetDCCToolsList(Param validate.GetDCCToolsListParam) (tools []model.ToolsFormat, err error) {
	var DCCToolsModel model.DCCTools
	var toolsModel model.Tools

	// 获取当前dcc下面工具所有工具ids
	dccToolsData, err := DCCToolsModel.GetDCCToolsList(Param)

	// 通过 tools_id 查询tools数据
	toolsIdsStr := ""
	toolsLen := len(dccToolsData)
	dccToolsMap := map[int64]int64{}
	for index, item := range dccToolsData {
		dccToolsMap[item.ToolsId] = item.Id
		toolsIdsStr += strconv.FormatInt(item.ToolsId, 10)
		if index < (toolsLen - 1) {
			toolsIdsStr += ","
		}
	}

	if toolsIdsStr != "" {
		tools, err = toolsModel.SelectToolsListByIds(toolsIdsStr)
		if err == nil {
			for toolsIndex, toolsItem := range tools {
				tools[toolsIndex].DccToolId = dccToolsMap[toolsItem.Id]
			}
		}
	}

	return
}
